package com.guigu.dh.controller;


import com.guigu.dh.model.Department;
import com.guigu.dh.model.Hospital;
import com.guigu.dh.model.Schedule;
import com.guigu.dh.model.vo.CheckSignKeyVo;
import com.guigu.dh.service.HospitalSetService;
import com.guigu.dh.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/hospitalSet/")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    // 验证签名key的方法
    @PostMapping("/checkSignKey")
    public Result checkSignKey(@RequestBody CheckSignKeyVo checkSignKeyVo){
        Boolean result = hospitalSetService.checkSignKey(checkSignKeyVo);
        if (result) {
            log.info("验证成功，医院编号：{}",checkSignKeyVo.getHoscode());
            return Result.ok();
        } else {
            log.error("错误验证，医院编号：{}，密匙：{}",checkSignKeyVo.getHoscode(),checkSignKeyVo.getSignKey());
            return Result.fail();
        }
    }

    // 接收图片的方法
    @PostMapping("/receiveFile")
    public Result receiveFile (MultipartFile file){
        if (file != null) {
            byte[] bytes = null;
            try {
                bytes = file.getBytes();

            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

            String base64 = Base64.getEncoder().encodeToString(bytes);
            return Result.ok(base64);
        }

        return Result.fail();
    }

    //保存医院信息的方法
    @PostMapping("/saveHospital")
    public Result saveHospital(@RequestBody Hospital hospital){
        Result result = hospitalSetService.saveHospital(hospital);
        log.info("成功添加医院信息，医院名称：{}，医院编号：{}",hospital.getHosname(),hospital.getHoscode());
        return result;
    }

    // 查询医院信息的方法
    @PostMapping("/showHospital")
    public Result showHospital(@RequestBody CheckSignKeyVo checkSignKeyVo) {
        return hospitalSetService.showHospital(checkSignKeyVo);
    }

    // 添加科室的方法
    @PostMapping("/addDepartment")
    public Result addDepartment(@RequestBody Department department){
        Boolean result = hospitalSetService.addDepartment(department);
        if (result) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 分页查询科室的方法
    @PostMapping("/pagingDepartment")
    public Result pagingDepartment(@RequestBody Map<String, String> map){
        return hospitalSetService.pagingDepartment(map);
    }

    // 移除科室的方法
    @DeleteMapping("/delDepartment/{depcode}")
    public Result delDepartmentByDepcode (@PathVariable String depcode){
        return hospitalSetService.delDepartmentByDepcode(depcode);
    }


    // 添加排班的方法
    @PostMapping("/addSchedule")
    public Result addSchedule(@RequestBody Schedule schedule){
        Boolean result = hospitalSetService.addSchedule(schedule);
        if (result) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 分页查询排班的方法
    @PostMapping("/pagingSchedule")
    public Result pagingSchedule(@RequestBody Map<String, String> map){
        return hospitalSetService.pagingSchedule(map);
    }

    // 移除排班的方法
    @DeleteMapping("/delSchedule/{depcode}/{hosScheduleId}")
    public Result delSchedule (@PathVariable String depcode,
                               @PathVariable String hosScheduleId){
        return hospitalSetService.delSchedule(depcode,hosScheduleId);
    }


}
